package com.ruoyi.web.controller.wxqy;

import com.ruoyi.common.annotation.ControllerAuthority;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.ErrorConstants;
import com.ruoyi.common.core.cache.Meta;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.NumberUtils;
import com.ruoyi.common.utils.StrUtils;
import com.ruoyi.qywx.model.vo.ContactGreetVo;
import com.ruoyi.qywx.service.inters.ContactPubGreetService;
import com.ruoyi.system.service.ISysTeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @author : hcl
 * @Date : 2023/8/28 11:23
 */
@RestController
@RequestMapping("/wxqy/greet")
@ControllerAuthority
public class ContactPubGreetController extends BaseController {
    @Autowired
    private ISysTeamService teamService;
    @Autowired
    private ContactPubGreetService greetService;

    @GetMapping("list")
    public AjaxResult gretList() {
        startPage();
        return success(getDataTable(greetService.greetList(null, getTeamId())));
    }

    @GetMapping("find")
    public AjaxResult getGreet(Long id) {
        if (NumberUtils.isFalse(id)) {
            return error(ErrorConstants.NO_DATA);
        }
        return greetService.findGreet(id, getTeamId());
    }

    @PostMapping("/add")
    public AjaxResult addGreet(@RequestBody @Valid ContactGreetVo greet) {
        String validate = greet.getWelcome().validate();
        if (StrUtils.isNotEmpty(validate)) {
            return error(validate);
        }
        Meta meta = teamService.selectTeamMeta(getTeamId());
        if (Objects.isNull(meta)) {
            return error(Constants.QY_WX_TEAM_ERR);
        }
        greet.setCreator(getUserId());
        return greetService.addGreet(greet, meta);
    }

    @PostMapping("/edit")
    public AjaxResult editGreet(@RequestBody @Valid ContactGreetVo greet) {
        String validate = greet.getWelcome().validate();
        if (StrUtils.isNotEmpty(validate)) {
            return error(validate);
        }
        Meta meta = teamService.selectTeamMeta(getTeamId());
        if (Objects.isNull(meta)) {
            return error(Constants.QY_WX_TEAM_ERR);
        }
        return greetService.editGreet(greet, meta);
    }

    @PostMapping("/delete")
    public AjaxResult deleteGreet(Long id) {
        if (NumberUtils.isFalse(id)) {
            return error(ErrorConstants.NO_DATA);
        }
        return greetService.deleteGreet(id, getTeamId());
    }
}
